package com.dzz.sjgl.utils;

import org.apache.commons.io.IOUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author KRIC
 */
public class Word2String {
    private static Logger logger = LoggerFactory.getLogger(Word2String.class);

//    public static void main(String[] args) {
//        String text = getText("C:\\Workspace\\p\\sjgl\\src\\main\\resources\\word\\2016年全国高考英语试题及答案(全国-II卷)精校版.doc");
//        text = text.replaceAll("[A-G][.]", "\r\n ❤. ");
//        text = SpliteTextInSentence.splitfuhao(text);
//        Set<String> sl = new HashSet<>(SpliteTextInSentence.testChunkSentences(text));
//        for (String row : sl) {
//            row = row.replaceAll("[0-9]{1,2}[.]", " ");
//            row = row.replaceAll("　", "");
//            row = row.replaceAll("●", "");
//            row = row.replaceAll("\\s{1,}", " ");
//            row = row.replaceAll(" [.]", ".");
//            row = row.replaceAll("[.]+|[?][.]", ".");
//            row = row.replace("例:", "");
//            String regEx = "[\\u4e00-\\u9fa5]";
//            String term = row.replaceAll(regEx, "aa");
//            int count = term.length() - row.length();
//            if (count > 2 || (row.indexOf("❤.") != row.lastIndexOf("❤."))) {
//                continue;
//            }
//            row = row.replace("❤.", "");
//            if (row.length() > 10 && row.length() < 600) {
////                System.out.println(row);
//            }
//        }


//        long t1 = System.currentTimeMillis();
//        Map<String, Integer> map = new TreeMap<>();
//
//        String pattern = "[a-zA-Z]+";
//
//        Pattern r = Pattern.compile(pattern);
//        Matcher m = r.matcher(text);
//        while (m.find()) {
//            String s = m.group();
//            if (StringUtils.isEmpty(s)) {
//                continue;
//            }
//            s = s.toLowerCase();
//            Integer times = map.get(s);
//            if (times == null) {
//                map.put(s, 1);
//            } else {
//                map.put(s, ++times);
//            }
//        }
//        long t3 = System.currentTimeMillis();
//
//        for (Map.Entry<String, Integer> stringIntegerEntry : map.entrySet()) {
//            System.out.println(stringIntegerEntry.getKey() + ":" + stringIntegerEntry.getValue());
//        }
//
//        System.out.println(t3 - t1);
//        System.out.println(map.size());
//    }

    public static String getText(String path) {
        path = path.toLowerCase();
        String text = null;
        FileInputStream stream = null;
        try {
            stream = new FileInputStream(new File(path));
            if (path.endsWith(".doc")) {
                HWPFDocument document = new HWPFDocument(stream);
                WordExtractor extractor = new WordExtractor(document);
                text = extractor.getText();
                extractor.close();
                document.close();
            } else if (path.endsWith(".docx")) {
                XWPFDocument document = new XWPFDocument(stream).getXWPFDocument();
                XWPFWordExtractor extractor = new XWPFWordExtractor(document);
                text = extractor.getText();
                extractor.close();
                document.close();
            } else {
                logger.error("此文件不是word文件:" + path);
            }
        } catch (IOException e) {
            logger.error(e.getMessage());
            return text;
        } finally {
            IOUtils.closeQuietly(stream);
        }
        return text;
    }
}
